package mfuny.ggongal.admin;

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

import mfuny.ggongal.admin.games.GameDao;
import mfuny.ggongal.admin.games.GameService;
import mfuny.ggongal.admin.memo.MemoDao;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;

@Controller
public class GamesController {
	
	GameService games;
	public GamesController() throws Exception {
		games = new GameService();
	}
	
	private static final Logger logger = LoggerFactory.getLogger(GamesController.class);
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String home(Locale locale, Model model) {
		logger.info("Welcome home! The client locale is {}.", locale);
		Date date = new Date();
		DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
		String formattedDate = dateFormat.format(date);
		model.addAttribute("serverTime", formattedDate );
		return "home";
	}
	
	@RequestMapping("/gamesList.ga")
	public ModelAndView gamesList( String pageNum ) throws Exception {
		return games.gamesList( pageNum );
	}
	
	@RequestMapping("/regGamesForm.ga")
	public ModelAndView regGamesForm() {
		return new ModelAndView("/games/regGamesForm.ga");
	}
	
	@RequestMapping("/regGames.ga")
	public ModelAndView regGames(@ModelAttribute GameDao gameDao ) throws Exception {
		games.gamesInsert( gameDao );
		return gamesList( null );
	}
	
	@RequestMapping("/updateGamesStatus.ga")
	public View updateGamesStatus(@ModelAttribute GameDao gameDao ) throws Exception {
		return games.gamesUpdateStatus( gameDao );
	}
	
	@RequestMapping("/gamesMemoPopup.ga")
	public ModelAndView gamesMemoPopup(@ModelAttribute MemoDao memoDao ) throws Exception {
		return games.gamesMemoPopup( memoDao );
	}
	
	@RequestMapping("/regGamesMemo.ga")
	public ModelAndView regGamesMemo(@ModelAttribute MemoDao memoDao ) throws Exception {
		return games.regGamesMemo( memoDao );
	}
	
	@RequestMapping("/searchGames.ga")
	public ModelAndView searchGames( String pageNum, String datepick1, String datepick2, String status, String no, String name ) throws Exception {
		return games.searchGames( pageNum, datepick1, datepick2, status, no, name );
	}
	
	@RequestMapping("/detailGames.ga")
	public ModelAndView detailGames( int no ) throws Exception {
		return games.detailGames( no );
	}
	
	@RequestMapping("/modifyGamesForm.ga")
	public ModelAndView modifyGamesForm( int no ) throws Exception {
		return games.modifyGamesForm( no );
	}
	
	@RequestMapping("/modifyGames.ga")
	public ModelAndView modifyGames(@ModelAttribute GameDao gameDao ) throws Exception {
		return games.modifyGames( gameDao );
	}
}
